package kines.ct;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.ArrayList;
/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

public class Agend {
  /**
   * Month of this Agend
   */
  private int month;
  /**
   * Year of this Agend
   */
  private int year;
  /**
   * Events of this Agend
   */
  private Object events[];
  /**
   * Default Constructor,it initializes month and year currently
   */
  public Agend(  HttpSession session )throws Exception{
    Calendar today = Calendar.getInstance();
    month = today.get( Calendar.MONTH );
    year = today.get( Calendar.YEAR );
    try{
      events = Event.getData( session, month, year);
    }catch( Exception ex ){
      throw new java.lang.UnsupportedOperationException("Fatal Error");
    }
  }
  /**
   * Contructor Agend from especiflic day
   */
  public Agend(  HttpSession session , Calendar from )throws Exception{
    try{
      events = Event.getData( session, from.get(Calendar.DATE), from.get(Calendar.MONTH), from.get(Calendar.YEAR) );
    }catch( Exception ex ){
      throw new java.lang.UnsupportedOperationException("Fatal Error");
    }
  }
  /**
   * Constuctor with period ( Year and Month )
   * @param month Month of this Agent
   * @param year Year of this Agent
   * @throws java.lang.Exception
   */
  public Agend( HttpSession session , int month, int year )throws Exception{
    if( month <= 12 && month >= 1 ){
      this.month = month;
      this.year = year;
      try{
        events = Event.getData( session, this.month, this.year );
      }catch (Exception ex) {
        throw new java.lang.UnsupportedOperationException("Fatal Error");
      }

    }else{
      throw new java.lang.UnsupportedOperationException("Mes incorrecto ( 1-12 )");
    }
  }
  /**
   * Gets Month of this Agend
   * @return int, month of this agend
   */
  public int getMonth( ){
    return month;
  }
  /**
   * Gets Year of this Agend
   * @return int, year of this agend
   */
  public int getYear( ){
    return year;
  }
  /**
   * Sets Month of this Agend
   * @param int month
   */
  public void setMonth( int month ){
    this.month = month;
  }
  /**
   * Sets Year of this Agend
   * @param int year
   */
  public void setYear( int year ){
    this.year = year;
  }
  /**
   * Gets events to this day
   * @param day to retrive events
   * @return Events for this day
   */
  public Object[] getEvents( int day ){
    ArrayList rEventos = new ArrayList( );
    for( int i = 0; i < events.length; i++ ){
      Event e = (Event)events[i];
      if( e.getNextContactDate().get( Calendar.DATE ) == day ){
        rEventos.add( e );
      }
    }
    return  rEventos.toArray();
  }
  /**
   * Gets events to this day, hour and minute
   * @param day
   * @param hour
   * @param minute
   * @return
   */
  public Object[] getEvents( int day , int hour, int minute ){
    ArrayList rEventos = new ArrayList( );
    for( int i = 0; i < events.length; i++ ){
      Event e = (Event)events[i];
      if( e.getNextContactDate().get( Calendar.DATE ) == day && e.getNextContactDate().get( Calendar.HOUR_OF_DAY ) == hour && e.getNextContactDate().get( Calendar.MINUTE ) == minute ){
        rEventos.add( e );
      }
    }
    return  rEventos.toArray();
  }

  /**
   * Gets events
   * @return events
   */
  public Object[] getEvents( ){
    return events;
  }

}